<?php

namespace UF\AccountBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use UF\AccountBundle\Entity\Account;
use UF\AccountBundle\Entity\Guest;
use UF\AccountBundle\Form\GuestType;
use UF\AccountBundle\Form\GuestHandler;

class GuestController extends Controller
{
    
    public function listGuestsAction($accountId)
    {	
    	$em = $this->getDoctrine()->getRepository('UFAccountBundle:Guest');
    	$guests = $em->findBy(array("account"=>$accountId));

    	$params = array("guests" => $guests);   	
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->render('UFAccountBundle:Guests:guestsList.html.twig', $params);    	    	
    }
    
    public function addGuestAction($accountId)
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();

    	$guest = new Guest();
    	$guestForm = $this->createForm(new GuestType($accountId), $guest);
    	$guestHandler = new GuestHandler($guestForm, $request, $em);
    	
    	$accountRepo = $this->getDoctrine()->getRepository('UFAccountBundle:Account');
    	$account = $accountRepo->findOneBy(array('id' => $accountId));
    	
    	try{
	    	if($guestHandler->process($account)){
	    		return $this->redirect($this->generateUrl("UFAccountBundle_listGuests", array('accountId' => $accountId)));
	    	}
    	}
    	catch(\Exception $e){throw $this->createNotFoundException("L'invité n'a pas pu être ajouté");}
  
	    $this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->render('UFAccountBundle:Guests:addGuest.html.twig', array("guestForm" => $guestForm->createView(),
    																			"accountId" => $accountId));
    }
    
    public function modifyGuestAction($guestId)
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	$repo = $this->getDoctrine()->getRepository('UFAccountBundle:Guest');

    	$guest = $repo->find($guestId);
    	$account = $guest->getAccount();
    	
    	$guestForm = $this->createForm(new GuestType($account->getId()), $guest);
    	$guestHandler = new GuestHandler($guestForm, $request, $em);

    	try{
    		if($guestHandler->process($account)){
    			return $this->redirect($this->generateUrl("UFAccountBundle_listGuests", array('accountId' => $account->getId())));
    		}
    	}
    	catch(\Exception $e){
    		throw $this->createNotFoundException("L'invité n'a pas pu être modifié");
    	}
    	
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->render('UFAccountBundle:Guests:modifyGuest.html.twig', array("guestForm" => $guestForm->createView(), 
    																				 "guestId" => $guestId));
    	
    }
    
    
    public function deleteGuestAction($guestId)
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	$repo = $this->getDoctrine()->getRepository('UFAccountBundle:Guest');
    	 
    	try
    	{
    		$guest = $repo->find($guestId);
    		$account = $guest->getAccount();
    		$em->remove($guest);
    		$em->flush();
    	}
    	catch (\Exception $e)
    	{
    		throw $this->createNotFoundException("L'invité n'a pas pu être supprimé");
    	}
    	 
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->redirect($this->generateUrl("UFAccountBundle_listGuests", array('accountId' => $account->getId())));
    }
    
}
